require "ISBaseObject"

QNW_TS_Task = ISBaseObject:derive("QNW_TS_Task")

local this = QNW_TS_Task
this.uuid = ""
this.name = ""
this.taskCategory = QNW_TS_TaskCategory
this.desc = ""
this.weight = 10
this.time = -1

function QNW_TS_Task:isComplete()
    return false
end

function QNW_TS_Task:complete()
    for k, v in pairs(self.rewards) do
        v:apply()
    end
    self:abandon()
end

function QNW_TS_Task:isAbandon()
    return true
end

function QNW_TS_Task:abandon()
    QNW_TS_ClientCore.abandonTask(self)
end

function QNW_TS_Task:isSave()
    return true
end

function QNW_TS_Task:save()
    return {}
end

function QNW_TS_Task:load(_table)
end

function QNW_TS_Task:loadReward(_table)
    self.rewards = {}
    for k, v in pairs(_table) do
        local reward = _G[v.Type]:new()
        reward:load(v)
        table.insert(self.rewards, reward)
    end
end

function QNW_TS_Task:getProgress()
    return ""
end

function QNW_TS_Task:registerClient(_player)
end

function QNW_TS_Task:unregisterClient(_player)
end

function QNW_TS_Task:getRewardText()
    local text = ""
    for i, v in ipairs(self.rewards) do
        text = text .. v:getText()
        if #self.rewards ~= 1 and i ~= #self.rewards then
            text = text .. ","
        end
    end
    return text
end

function QNW_TS_Task:isTime()
    if self.time == -1 then
        return true
    else
        return getGameTime():getCalender():getTimeInMillis() < self.time
    end
end

function QNW_TS_Task:getTime()
    if self.time == -1 then
        return getText("IGUI_QNW_TaskSystem_Window_NoTime")
    end
    local now = getGameTime():getCalender():getTimeInMillis()
    local time = self.time - now
    local minute = math.floor(time / 1000 / 60 % 60)
    local hour = math.floor(time / 1000 / 60 / 60 % 24)
    local day = math.floor(time / 1000 / 60 / 60 / 24 % 24)
    local text = ""
    if day ~= 0 then
        text = text .. string.format("%d%s", day, getText("IGUI_Gametime_day"))
    end
    if hour ~= 0 or day ~= 0 then
        text = text .. string.format("%d%s", hour, getText("IGUI_Gametime_hour"))
    end
    if minute ~= 0 or hour ~= 0 or day ~= 0 then
        text = text .. string.format("%d%s", minute, getText("IGUI_Gametime_minute"))
    end
    return text
end

function QNW_TS_Task:new()
    local o = {}
    setmetatable(o, self)
    self.__index = self

    o.rewards = {}
    o.uuid = getRandomUUID()

    return o
end

function QNW_TS_Task:initReward()
end